package com.ruoyi.module.yzx.controller;

import com.alibaba.fastjson2.function.impl.ToLong;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.module.yzx.tool.QrCodeUtils;
import com.ruoyi.system.domain.YhGasCylinders;
import com.ruoyi.system.service.IYhGasCylindersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 气瓶信息Controller
 *
 * @author yinhe
 * @date 2022-11-07
 */
@RestController
@RequestMapping("/yzx/cylinders")
public class YzxGasCylindersController extends BaseController {
    @Autowired
    private IYhGasCylindersService yhGasCylindersService;

    /**
     * 查询气瓶信息列表
     */
    @PreAuthorize("@ss.hasPermi('yzx:cylinders:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhGasCylinders yhGasCylinders) {
        startPage();
        List<YhGasCylinders> list = yhGasCylindersService.selectYhGasCylindersList(yhGasCylinders);
        return getDataTable(list);
    }

    /**
     * 导出气瓶信息列表
     */
    @PreAuthorize("@ss.hasPermi('yzx:cylinders:export')")
    @Log(title = "气瓶信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhGasCylinders yhGasCylinders) {
        List<YhGasCylinders> list = yhGasCylindersService.selectYhGasCylindersList(yhGasCylinders);
        ExcelUtil<YhGasCylinders> util = new ExcelUtil<YhGasCylinders>(YhGasCylinders.class);
     /*   util.hideColumn(
                "note3","note4","note5"
        );*/
        util.exportExcel(response, list, "气瓶信息数据");
    }

    /**
     * 获取气瓶信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('yzx:cylinders:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(yhGasCylindersService.selectYhGasCylindersById(id));
    }

    /**
     * 新增气瓶信息
     */
    /*@PreAuthorize("@ss.hasPermi('yzx:cylinders:add')")
    @Log(title = "气瓶信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhGasCylinders yhGasCylinders)
    {
        return toAjax(yhGasCylindersService.insertYhGasCylinders(yhGasCylinders));
    }*/
    @PreAuthorize("@ss.hasPermi('yzx:cylinders:add')")
    @GetMapping("/pltj/{num}")
    public void adds(@PathVariable("num") Long num) throws Exception {

        for (int i = 0; i < num; i++) {
            YhGasCylinders yhGasCylinders = new YhGasCylinders();
            // 获取当前登录用户
           /* LoginUser loginUser = getLoginUser();
            SysUser sysUser = loginUser.getUser();
            // 存入userId和deptId，做数据权限
            yhGasCylinders.setUserId(sysUser.getUserId());
            yhGasCylinders.setDeptId(sysUser.getDeptId());*/
            yhGasCylinders.setNote1("1");
            yhGasCylinders.setNote3("1");
            yhGasCylindersService.insertYhGasCylinders(yhGasCylinders);
        }

    }

    /**
     * 修改气瓶信息
     */
    @PreAuthorize("@ss.hasPermi('yzx:cylinders:edit')")
    //@Log(title = "气瓶信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhGasCylinders yhGasCylinders) {
        /*LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        // 存入userId和deptId，做数据权限
        yhGasCylinders.setUserId(sysUser.getUserId());
        yhGasCylinders.setDeptId(sysUser.getDeptId());
        yhGasCylinders.setBelongingGasStation(sysUser.getDeptId().toString());
        yhGasCylinders.setWorkPlace(sysUser.getDeptId().toString());
        yhGasCylinders.setUserName(sysUser.getUserName());*/
        yhGasCylinders.setNote1("2");
        return toAjax(yhGasCylindersService.updateYhGasCylindersyzx(yhGasCylinders));
    }

    /**
     * 删除气瓶信息
     */
    @PreAuthorize("@ss.hasPermi('yzx:cylinders:remove')")
    @Log(title = "气瓶信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(yhGasCylindersService.deleteYhGasCylindersByIds(ids));
    }

    //    @PostMapping("/QR/cs")
//    public void export1(HttpServletResponse response) {
//        List<Map<String, String>> paths = new ArrayList<Map<String, String>>();
//
//        for (int i = 0; i < id.length; i++) {
//            Long j = id[i];
//            String l = j.toString();
//            /*String text = "/pages/sys/workbench/add-form1?id=" + l;
//            String logoPath = null;*//*
//            String destPath = "D:\\QRCode\\no." + l + ".jpg";
//            */
//            //QrCodeUtils.encode(text, logoPath, destPath, true);*//*
//            HashMap<String, String> map = new HashMap<>();
//            map.put("filePath",destPath);
//            paths.add(map);
//        }
//        try {
//            QrCodeUtils.download(paths,"二维码",response);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }
    @PreAuthorize("@ss.hasPermi('yzx:cylinders:qr')")
    @PostMapping("/QR")
    public AjaxResult QRcode(HttpServletResponse response) throws Exception {
        List<Map<String, String>> paths = new ArrayList<Map<String, String>>();
        YhGasCylinders yh = new YhGasCylinders();
        yh.setNote3("1");
        List<YhGasCylinders> list = yhGasCylindersService.selectYhGasCylindersList(yh);
        for (int i = 0; i < list.size(); i++) {
            Long j = list.get(i).getId();
            String l = j.toString();
            String text = "/pages/sys/workbench/add-form1?id=" + l;
            String text1=co(text);
            String logoPath = null;
            String destPath = RuoYiConfig.getDownloadPath()+"\\QRCode\\no." + l + ".jpg";
            QrCodeUtils.encode(text1, logoPath, destPath, true);
            list.get(i).setNote3("2");
            yhGasCylindersService.updateYhGasCylindersyzx(list.get(i));
            HashMap<String, String> map = new HashMap<>();
            map.put("filePath", destPath);
            paths.add(map);
        }
        try {
            QrCodeUtils.download(paths, "二维码", response);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return AjaxResult.success();
    }
    public static String co(String text )
    {
        byte[] bt=text.getBytes(); //将需要加密的内容转换为字节数组
        for(int i=0;i<bt.length;i++)
        {
            bt[i]=(byte)(bt[i]^10); //通过异或运算进行加密
        }
        String newresult=new String(bt,0,bt.length); //将加密后的字符串保存到 newresult变量中
        return newresult;
    }
   /* @GetMapping("/QR/cs")
    public void QRcode1(@PathVariable Long[] id,HttpServletResponse response) throws Exception {
        List<Map<String, String>> paths = new ArrayList<Map<String, String>>();

        for (int i = 0; i < id.length; i++) {
            Long j = id[i];
            String l = j.toString();
            *//*String text = "/pages/sys/workbench/add-form1?id=" + l;
            String logoPath = null;*//*
            String destPath = "D:\\QRCode\\no." + l + ".jpg";
         *//*   QrCodeUtils.encode(text, logoPath, destPath, true);*//*
            HashMap<String, String> map = new HashMap<>();
            map.put("filePath",destPath);
            paths.add(map);
        }
        try {
            QrCodeUtils.download(paths,"二维码",response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/
}
